package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.EvaluationLogFormalDao;
import cn.topevery.garden.entity.po.EvaluationLogFormal;
import cn.topevery.garden.entity.query.EvaluationLogFormalQuery;
import cn.topevery.garden.entity.vo.EvaluationLogFormalVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 检查考评上报记录表
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class EvaluationLogFormalDaoImpl extends BaseDaoImpl<EvaluationLogFormal> implements EvaluationLogFormalDao {
    @Override
    public <P extends DbCsp> List<EvaluationLogFormal> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<EvaluationLogFormalVO> listPage(EvaluationLogFormalQuery query) {
        String table = "t_evaluation_log_formal telf";

        String columns = "telf.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("telf.c_id desc");
        sqlQueryMate
                .where(true, "telf.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, EvaluationLogFormalVO.class);
    }

    @Override
    public EvaluationLogFormal getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public void statisticsLogAdd(Map<String, Object> map) {
        String sql = "INSERT INTO t_evaluation_log_formal(c_log_id, c_report_table_id, c_log_date, c_dept_id, c_dept_name, c_rule_id, c_item_id,\n" +
                "                                            c_item_name, c_class_id, c_class_name, c_class_score, c_code, c_user_id,\n" +
                "                                            c_user_name, c_rule_name, c_counts, c_unit, c_desc, c_address, c_dock_score,\n" +
                "                                            c_rule_score, c_lng, c_lat, c_remark, c_db_create_id, c_db_create_date,\n" +
                "                                            c_db_update_id, c_db_update_date, c_db_status)\n" +
                "        select c_id,\n" +
                "               :tableId,\n" +
                "               c_log_date,\n" +
                "               c_dept_id,\n" +
                "               c_dept_name,\n" +
                "               c_rule_id,\n" +
                "               c_item_id,\n" +
                "               c_item_name,\n" +
                "               c_class_id,\n" +
                "               c_class_name,\n" +
                "               c_class_score,\n" +
                "               c_code,\n" +
                "               c_user_id,\n" +
                "               c_user_name,\n" +
                "               c_rule_name,\n" +
                "               c_counts,\n" +
                "               c_unit,\n" +
                "               c_desc,\n" +
                "               c_address,\n" +
                "               c_dock_score,\n" +
                "               c_rule_score,\n" +
                "               c_lng,\n" +
                "               c_lat,\n" +
                "               c_remark,\n" +
                "               c_db_create_id,\n" +
                "               c_db_create_date,\n" +
                "               c_db_update_id,\n" +
                "               c_db_update_date,\n" +
                "               0\n" +
                "        from t_evaluation_log log\n" +
                "        where log.c_log_date >= :beginDate\n" +
                "          and log.c_log_date <= :endDate\n" +
                "          and log.c_db_status = 0";
        run(sql, CommandType.Text, dbCommand -> {
            dbCommand.addInParameter("tableId", map.get("tableId"));
            dbCommand.addInParameter("beginDate", map.get("beginDate"));
            dbCommand.addInParameter("endDate", map.get("endDate"));
            dbCommand.executeNonResultSet();
        });
    }

    @Override
    public List<EvaluationLogFormal> getFormalReportList(Map<String, Object> map) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT * from t_evaluation_log_formal where c_db_status = 0 and c_report_table_id ='"+map.get("tableId").toString()+"' and c_rule_id = '"+map.get("ruleId").toString() +"'");

        AtomicReference<List<EvaluationLogFormal>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(EvaluationLogFormal.class));
        });

        return vo.get();
    }
}
